import main from '../../../main';
import { clearEmptyKey } from '../../../utils/utils';
import { Dict, DictData } from './types';
import download from '../../../utils/download';

// 列表
export async function apiGetDictList(pageNum: number, pageSize: number, params?: any) {
  let tParams = params ? clearEmptyKey(params) : null;
  let res = await main.api.get('/system/dict/type/list', { params: { pageNum, pageSize, ...tParams } });

  return res;
}

// 详情
export async function apiGetDictDetail(dictId: string) {
  let res = await main.api.get(`/system/dict/type/${dictId}`);

  return res;
}

// 更新
export async function apiUpdateDictDetail(dictId: string, data: Dict) {
  let fn = dictId === 'create' ? main.api.post : main.api.put;
  let res = await fn(`/system/dict/type`, data);

  return res;
}

// 删除
export async function apiDeleteDictDetail(dictId: string) {
  let res = await main.api.delete(`/system/dict/type/${dictId}`);

  return res;
}
// 刷新缓存
export async function apiRefreshDict() {
  let res = await main.api.delete('/system/dict/type/refreshCache');

  return res;
}
// 字典数据列表
export async function apiGetDictDataList(type: string) {
  let res = await main.api.get(`/system/dict/data/list?dictType=${type}&orderByColumn=createTime&isAsc=desc`);

  return res;
}
// 字典数据详情
export async function apiGetDictDataDetail(id: string) {
  let res = await main.api.get(`/system/dict/data/${id}`);

  return res;
}
// 删除字典数据
export async function apiDeleteDictDataDetail(id: string) {
  let res = await main.api.delete(`/system/dict/data/${id}`);

  return res;
}
// 保存字典数据
export async function apiUpdateDictDataDetail(id: string, data: DictData) {
  let fn = id === 'create' ? main.api.post : main.api.put;
  let res = await fn(`/system/dict/data`, data);

  return res;
}
// 导出
export async function apiExportDictType(params: any) {
  await download('/system/dict/type/export', params, `dictType_${new Date().getTime()}.xlsx`);
}
// 导出字典值
export async function apiExportDictData(params: any) {
  await download('/system/dict/data/export', params, `dictData_${new Date().getTime()}.xlsx`);
}
